<?php
namespace app\controllers;

use app\models\Question;

class QuestionsController extends \lithium\action\Controller {
	public function index() {
		$questions = Question::all();
		return compact('questions');
	}	
	
	public function add() {
		$success = false;

		if ($this->request->data) {
			$post = Question::create($this->request->data);
			$success = $post->save();
		}
		return compact('success');
	}
	
	public function edit() {
		$success = false;
		$id = $this->request->params['id'];
		
		
		$question = Question::find($id);
				
		if (empty($question)) {
			$this->redirect(array('controller' => 'Questions'));	
		}
			
		if ($this->request->data) {
			if ($question->save($this->request->data)) {
				$this->redirect(array('controller' => 'Questions'));
			}			
		}
		
		$title = "Edit Question";
		return compact('question', 'title');
	}
	
	public function delete() {
		$question = Question::find('all', array('conditions' => array('id' => $this->request->params['id'])));
		$question->delete();
		
		$this->redirect(array('controller' => 'Questions'));
	}
}